package cn.ljy.common.utils.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class StringRedisUtils {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 判断缓存中是否有对应的key
     *
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        return stringRedisTemplate.hasKey(key);
    }

    /**
     * 获取key值的过期时间
     *
     * @param key
     * @return
     */
    public Long getExpire(final String key) {
        return stringRedisTemplate.getExpire(key);
    }

    /**
     * 延长key值的过期时间（以秒问单位）
     * @param key
     * @param timeout  过期的时长
     * @param timeUnit 时间单位
     * @return
     */
    public void expire(final String key, final long timeout, TimeUnit timeUnit) {
        stringRedisTemplate.expire(key, timeout, TimeUnit.SECONDS);
    }

    /**
     * 写入缓存,字符串类型
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, String value) {
        boolean result = false;
        try {
            ValueOperations operations = stringRedisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 写入缓存设置时效时间
     *
     * @param key
     * @param value
     * @param expireTime 有效时长，只是数值
     * @param timeUnit   有效时长的单位
     * @return
     */
    public boolean set(final String key, String value, Long expireTime, TimeUnit timeUnit) {
        boolean result = false;
        try {
            ValueOperations operations = stringRedisTemplate.opsForValue();
            operations.set(key, value);
            stringRedisTemplate.expire(key, expireTime, timeUnit);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 写入缓存设置时效时间
     *
     * @param key
     * @param value
     * @param expiresDate 过期的时间
     * @return
     */
    public boolean set(final String key, String value, Date expiresDate) {
        boolean result = false;
        try {
            ValueOperations operations = stringRedisTemplate.opsForValue();
            operations.set(key, value);
            stringRedisTemplate.expireAt(key, expiresDate);//设置过期日期
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 批量删除对应的value
     *
     * @param keys 可以写多个
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    /**
     * 批量删除key
     *
     * @param pattern 匹对规则
     */
    public void removePattern(final String pattern) {
        Set<String> keys = stringRedisTemplate.keys(pattern);
        if (keys.size() > 0) {
            stringRedisTemplate.delete(keys);
        }
    }

    /**
     * 删除对应的value
     *
     * @param key
     */
    public void remove(final String key) {
        if (exists(key)) {
            stringRedisTemplate.delete(key);
        }
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public String get(final String key) {
        ValueOperations operations = stringRedisTemplate.opsForValue();
        return (String)operations.get(key);
    }

    /**
     * 重名名key，如果newKey已经存在，则newKey的原值被覆盖
     *
     * @param oldKey
     * @param newKey
     */
    public void renameKey(String oldKey, String newKey) {
        stringRedisTemplate.rename(oldKey, newKey);
    }

    /**
     * newKey不存在时才重命名
     *
     * @param oldKey
     * @param newKey
     * @return 修改成功返回true
     */
    public boolean renameKeyIfNewKeyNotExist(String oldKey, String newKey) {
        return stringRedisTemplate.renameIfAbsent(oldKey, newKey);
    }


    /**
     * 写入缓存,map
     * @parm map map集合的名称
     * @param key
     * @param value
     * @return
     */
//    public boolean set(final String map,final Object key, Object value) {
//        boolean result = false;
//        try {
//            HashOperations<String, Object, Object> hashOperations = stringRedisTemplate.opsForHash();
//            hashOperations.put(map,key,value);
//            result = true;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return result;
//    }
}
